package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项目管理
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference  //查找服务
    private MemberService memberService;

    //新增会员
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try {
            memberService.add(member);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
        return  new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = memberService.pageQuery(queryPageBean);
        return result;
    }

    //删除会员
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            memberService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
        return  new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    //编辑会员信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {

        try {
            memberService.edit(member);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    //通过ID查找会员信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Member member = null;
        try {
            member = memberService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,member);
    }

    /*//查询所有检查项
    @RequestMapping("/findAll")
    public Result findAll(){
       List<CheckItem> checkItemList= checkItemService.findAll();
       if (checkItemList!=null && checkItemList.size()>0){
           Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
           result.setData(checkItemList);
           return result;
       }
       return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }*/
}
